package com.yuncheng.spcyApi.alog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.entity.SysOperLog;
import com.yuncheng.spcyApi.alog.enums.LogTypeConstant;
import com.yuncheng.spcyApi.alog.service.ISysOperLogService;
import com.yuncheng.spcyApi.alog.utils.StringEmptyUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "接口请求日志")
@RestController
@RequestMapping("/api/spzx-spcy/operLog")
public class LogController {

    @Resource
    private ISysOperLogService sysOperLogService;

   /* @Log(title = "测试")
    @GetMapping(value = "getSelect")
    public HttpResult getSelect(@RequestParam(value = "id",defaultValue = "")String id){
        System.out.println(id);
        return HttpResult.ok("成功");
    }*/

    @GetMapping(value = "pageList")
    public HttpResult pageList(SysOperLog log,
                               @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                               @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                               OrderItemVo orderItemVo){
       Page<SysOperLog> page = new Page<>(pageNo, pageSize);

       LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();

       queryWrapper.like(StringEmptyUtils.isNotBlank(log.getRequestMethod()), SysOperLog::getRequestMethod, log.getRequestMethod());
       queryWrapper.like(StringEmptyUtils.isNotBlank(log.getIp()), SysOperLog::getIp, log.getIp());
       queryWrapper.like(StringEmptyUtils.isNotBlank(log.getTitle()), SysOperLog::getTitle, log.getTitle());
       queryWrapper.like(StringEmptyUtils.isNotBlank(log.getMethod()), SysOperLog::getMethod, log.getMethod());
       queryWrapper.like(StringEmptyUtils.isNotBlank(log.getUsername()), SysOperLog::getUsername, log.getUsername());
       queryWrapper.like(StringEmptyUtils.isNotBlank(log.getRequestParam()), SysOperLog::getRequestParam, log.getRequestParam());
       queryWrapper.eq(SysOperLog::getLogType, LogTypeConstant.Log_Type);

       if (StringEmptyUtils.isNotBlank(orderItemVo.getOrderItem())) {
           OrderItemUtils.setOrderItem(queryWrapper,orderItemVo);
       }else {
           queryWrapper.orderByDesc(SysOperLog::getCreateTime);
       }
        IPage<SysOperLog> pageList = sysOperLogService.page(page, queryWrapper);

       return HttpResult.ok(pageList);
   }

}
